using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SOEI.Solution.Application;
using SOEI.Solution;
using SOEI.Solution.Integratedes.ElectronicSignet;
using SOEI.Solution.Integratedes.ElectronicSignet.DomainService;
using SOEI.Solution.Integratedes.ElectronicSignet.Dtos;


using SOEI.Solution.Authorization;

namespace SOEI.Solution.Integratedes.ElectronicSignet
{
	/// <summary>
	/// 【扩展模块】  <br/>
	/// 【功能描述】  ：XXX 模块<br/>
	/// 【创建日期】  ：2020.05.21 <br/>
	/// 【开发人员】  ：static残影<br/>
	///</summary>
	[ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
	public class PledgeBookAppService : SolutionAppServiceBase, IPledgeBookAppService
	{
		/// <summary>
		///【PledgeBook】仓储层
		/// </summary>
		private readonly IRepository<PledgeBook, long>	_pledgebookRepository;

		/// <summary>
		///【PledgeBook】领域服务
		/// </summary>
		private readonly IPledgeBookManager _pledgebookManager;
		
		public PledgeBookAppService(
			IRepository<PledgeBook, long>  pledgebookRepository,
			IPledgeBookManager pledgebookManager
		)
		{
			_pledgebookRepository = pledgebookRepository;
			_pledgebookManager=pledgebookManager;
		}
		
		#region -------------------------------------------------辅助工具生成---------------------------------------------- 
		
		/// <summary>
		///【PledgeBook】获取的分页列表信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task<PagedResultDto<PledgeBookListDto>> GetPaged(GetPledgeBooksInput input)
		{
			var query = _pledgebookRepository.GetAll()
							//模糊搜索 字段JGBH
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JGBH.Contains(input.FilterText))                                                                                      
							//模糊搜索 字段Remark
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Remark.Contains(input.FilterText))                                                                                      
			;
			// TODO:根据传入的参数添加过滤条件

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			var entityListDtos = ObjectMapper.Map<List<PledgeBookListDto>>(entityList);

			return new PagedResultDto<PledgeBookListDto>(count,entityListDtos);
		}

		/// <summary>
		///【PledgeBook】通过指定id获取MemberListDto信息
		/// </summary>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task<PledgeBookListDto> GetById(EntityDto<long> input)
		{
			var entity = await _pledgebookRepository.GetAsync(input.Id);

			var dto = ObjectMapper.Map<PledgeBookListDto>(entity);
			return dto;
		}

		/// <summary>
		///【PledgeBook】 获取编辑
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task<GetPledgeBookForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetPledgeBookForEditOutput();
			PledgeBookEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _pledgebookRepository.GetAsync(input.Id.Value);
				editDto = ObjectMapper.Map<PledgeBookEditDto>(entity);
			}
			else
			{
				editDto = new PledgeBookEditDto();
			}
			output.PledgeBook = editDto;
			return output;
		}
		/// <summary>
		///【PledgeBook】 添加或者修改的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task CreateOrUpdate(CreateOrUpdatePledgeBookInput input)
		{
			
			if (input.PledgeBook.Id.HasValue)
			{
				await Update(input.PledgeBook);
			}
			else
			{
				await Create(input.PledgeBook);
			}
		}
		/// <summary>
		///【PledgeBook】新增
		/// </summary>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		protected virtual async Task<PledgeBookEditDto> Create(PledgeBookEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

			var entity = ObjectMapper.Map<PledgeBook>(input);
			//调用领域服务
			entity = await _pledgebookManager.CreateAsync(entity);

			var dto=ObjectMapper.Map<PledgeBookEditDto>(entity);
			return dto;
		}

		/// <summary>
		///【PledgeBook】编辑
		/// </summary>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		protected virtual async Task Update(PledgeBookEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新
			var key =  input.Id.Value;
			var entity = await _pledgebookRepository.GetAsync(key);
			//  input.MapTo(entity);
			//将input属性的值赋值到entity中
			ObjectMapper.Map(input, entity);
			await _pledgebookManager.UpdateAsync(entity);
		}

		/// <summary>
		///【PledgeBook】删除信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _pledgebookManager.DeleteAsync(input.Id);
		}

		/// <summary>
		///【PledgeBook】 批量删除Member的方法
		/// </summary>
		//[AbpAuthorize(PledgeBookPermissions.Node)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _pledgebookManager.BatchDelete(input);
		}

		#endregion

		#region -------------------------------------------------用户自定义------------------------------------------------
		/*请在此扩展应用服务实现*/
		#endregion
	}
}
